package china.accountinghelper.activity;

import com.xdroid.base.AsyncTaskBase;

import china.wc.tallyhelper.R;
import china.wc.tallyhelper.adapter.AdapterListViewCategoryList;
import china.wc.tallyhelper.business.BusinessCategory;
import china.wc.tallyhelper.controls.SlideMenu;
import china.wc.tallyhelper.controls.SlideMenuItem;
import china.wc.tallyhelper.model.ModelCategory;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class ActivityCategory extends ActivityFrame implements OnClickListener
{
    static final String                 EDITING_MODEL_OBJECT            = "ModelCategory";
    static final int                    REQUEST_CODE_ADD_CATEGORY       = 1;
    static final int                    REQUEST_CODE_EDIT_CATEGORY      = 2;
    static final int                    RESULT_ERR                      = RESULT_FIRST_USER + 1;
    static final int                    DIALOG_CONFIRM_CATEGORY_DELETE  = 1;

    private ListView                    mFramebodyCategory_ListView_CategoryList;
    private AdapterListViewCategoryList mAdapterCategoryListView;
    private BusinessCategory            mBusinessCategory;
    // 当前正在操作的实体类对象
    private ModelCategory               mSelectedModelCategory;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 载入主页面Layout文件
        loadFramebody(R.layout.framebody_category);
        // 生成主界面的SlideMenu菜单
        creatSlideMenu(R.array.slidemenu_category);
        // 设置标题
        setTopTitle(R.string.title_category);

        executeInitTask(null);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (mAdapterCategoryListView!=null) {
            mAdapterCategoryListView.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        mAdapterCategoryListView.notifyDataSetChanged();

        if (requestCode == REQUEST_CODE_ADD_CATEGORY) {
            if (resultCode == RESULT_OK) {
                showToastShort(R.string.message_category_add_ok);
            } else if (resultCode == RESULT_ERR) {
                showToastShort(R.string.message_category_add_err);
            }

        } else if (requestCode == REQUEST_CODE_EDIT_CATEGORY) {
            if (resultCode == RESULT_OK) {
                showToastShort(R.string.message_category_update_ok);
            } else if (resultCode == RESULT_ERR) {
                showToastShort(R.string.message_category_update_err);
            }
        }

    }

    /**
     * 生成类别管理页面的上下文菜单。
     * 
     */
    @Override
    public void onCreateContextMenu(ContextMenu pMenu, View pView, ContextMenuInfo pMenuInfo)
    {
        super.onCreateContextMenu(pMenu, pView, pMenuInfo);
        getMenuInflater().inflate(R.menu.contextmenu_categorylist, pMenu);

        AdapterView.AdapterContextMenuInfo _MenuInfo = (AdapterView.AdapterContextMenuInfo) pMenuInfo;
        mSelectedModelCategory = mAdapterCategoryListView.getItem(_MenuInfo.position);

        pMenu.setHeaderIcon(R.drawable.icon_xxjx_sbzz)
            .setHeaderTitle(mSelectedModelCategory.getName());

    }

    /**
     * 上下文菜单点击事件
     */
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        // 修改类别
        if (item.getItemId() == R.id.ContextMenu_CategoryList_EditCategory) {
            Bundle _Bundle = new Bundle();
            _Bundle.putSerializable(EDITING_MODEL_OBJECT, mSelectedModelCategory);
            openActivityForResult(ActivityCategoryAddAndEdit.class, _Bundle, REQUEST_CODE_EDIT_CATEGORY);
            
        // 删除类别
        } else if (item.getItemId() == R.id.ContextMenu_CategoryList_DeleteCategory) {
            showDialog(DIALOG_CONFIRM_CATEGORY_DELETE);
        }
        
        mAdapterCategoryListView.notifyDataSetChanged();
        return super.onContextItemSelected(item);
    }

    @Override
    protected Dialog onCreateDialog(int pDialogId)
    {
        AlertDialog.Builder _AlertDialogBuilder = new AlertDialog.Builder(this);
    
        // 删除警告对话框
        if (pDialogId == DIALOG_CONFIRM_CATEGORY_DELETE) {
            _AlertDialogBuilder.setTitle(R.string.title_category_delete)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(getString(R.string.message_category_check_delete))
                    .setPositiveButton(R.string.ok, this)
                    .setNegativeButton(R.string.cancel, this);
        }
    
        return _AlertDialogBuilder.create();
    }

    /**
     * Dialog按钮的点击事件
     */
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        // 删除警告对话框
        if (getShowingDialogID() == DIALOG_CONFIRM_CATEGORY_DELETE) {
            // 确定按钮
            if (which == DialogInterface.BUTTON_POSITIVE) {
                mBusinessCategory.deleteModel(mSelectedModelCategory);
            // 取消按钮
            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                dialog.cancel();
            }
        }
    
        mAdapterCategoryListView.notifyDataSetChanged();
    }

    /**
     * SlideMenu的点击事件
     */
    @Override
    public void onSlideMenuItemClick(SlideMenu pSlideMenu, SlideMenuItem pSlideMenuItem, AdapterView<?> pParent, View pView, int pPosition, long pId)
    {
        if (pPosition == 0) {
            slideMenuToggle();
            openActivityForResult(ActivityCategoryAddAndEdit.class, null, REQUEST_CODE_ADD_CATEGORY);
        }
    }

    @Override
    protected void initFields()
    {
        mAdapterCategoryListView = new AdapterListViewCategoryList(this);
        mBusinessCategory = new BusinessCategory();
    }

    @Override
    protected void initViews()
    {
        mFramebodyCategory_ListView_CategoryList = (ListView) this.findViewById(R.id.FramebodyCategory_ListView_CategoryList);
        mFramebodyCategory_ListView_CategoryList.setEmptyView(findViewById(R.id.FramebodyCategory_LinearLayout_Empty));
    }

    @Override
    protected void initListeners()
    {
    }

    @Override
    protected void bindData()
    {
        mFramebodyCategory_ListView_CategoryList.setAdapter(mAdapterCategoryListView);
    }

    @Override
    protected void register()
    {
        registerForContextMenu(mFramebodyCategory_ListView_CategoryList);
    }

    @Override
    protected Object doInBackground(Object... params)
    {
        initFields();
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        super.onPostExecute(result);
        initViews();
        initListeners();
        bindData();
        register();
    }
    
}
